// spGuiListBoxNED.js

// our javascripts namespace
var spGuiListBoxNED = {};

///////////////////////////////////////////////////////////////////////////////////////////////////
// init
///////////////////////////////////////////////////////////////////////////////////////////////////

spGuiListBoxNED.init = function(){

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle when the user clicks on a list item
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery( 'body' ).on( 'click', '.updateinfo', function(e) {
	container 		= jQuery(this).closest('.spGuiListBoxNED');

	container.find('.spEdit').removeAttr( 'disabled' );	
	container.find('.spDelete').removeAttr( 'disabled' );
	container.find('.spTrash').removeAttr( 'disabled' );
	
	infoselector		= container.attr( 'data-infoselector' );
	ajaxinfo			= container.attr( 'data-ajaxinfo' );
	db					= container.attr( 'data-database' );
	fb					= container.attr( 'data-formbuilder' );
	subheading			= container.attr( 'data-subheading' );
	formbuilderlayout	= container.attr( 'data-formbuilderlayout' );
	spShowLoadingBar( infoselector );

	jQuery( infoselector ).load(
		spHome+'/wp-admin/admin-ajax.php',
		{
			action				: 'sp_ajax_response',
			formType			: 'loadonly',
			loadonly			: ajaxinfo,
			id 					: jQuery(this).attr('data-id'),
			database			: db,
			formbuilder			: fb,
			subheading			: subheading,
			formbuilderlayout	: formbuilderlayout
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
			spInit();
		}
	);
});	

///////////////////////////////////////////////////////////////////////////////////////////////////
// initialize listbox
///////////////////////////////////////////////////////////////////////////////////////////////////

function spGuiUpdateListBox( listboxinfo, db /*, dbtitle*/ )
{
	listboxinfo.load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: "ajaxList",
			database		: db
			//databasetitle	: dbtitle
		}
	);
}

jQuery( '.spGuiListBoxNED' ).each(function(){
	listboxinfo	= jQuery(this).find( '.spListBoxInfo' );
	db			= jQuery(this).attr( 'data-database' );
//	dbtitle		= jQuery(this).attr( 'data-database-title' );
	
	listboxinfo.html( "<img src='"+spHome+"/wp-content/plugins/sigmaphp/images/ajax-loader.gif' alt=''/>" );
	
	spGuiUpdateListBox( listboxinfo, db /*, dbtitle*/ );
	jQuery(this).find('.spEdit').attr( 'disabled', 'disabled' );	// disable edit button
	jQuery(this).find('.spDelete').attr( 'disabled', 'disabled' );	// disable delete button
	jQuery(this).find('.spTrash').attr( 'disabled', 'disabled' );	// disable delete button
});

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle trash
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery( 'body' ).on( 'click', '.spGuiListBoxNED .spTrash', function(e) {

	container = jQuery(this).closest('.spGuiListBoxNED');
	
	infoselector	= container.attr( 'data-infoselector' );
	db				= container.attr( 'data-database' );
//	dbtitle			= container.attr( 'data-database-title' );
	listboxinfo		= container.find( '.spListBoxInfo' );
	sel 			= container.find( '.spGuiListBoxItem[data-selected=true]' );
	name 			= sel.attr( 'data-name' );
	id 				= sel.attr( 'data-id' );
	
	if ( confirm( 'Are you sure you want to trash: '+name ) )
	{
		// delete
		spAjaxCallback({ 
			callback 	: "spGuiListBoxNEDTrash", 
			param		: [
					{ name:'id',		value:id },
					{ name:'database', 	value:db }
				],
			title		: 'Moving to trash bin...',
			success		: function(){
				listboxinfo.html( "<img src='"+spHome+"/wp-content/plugins/sigmaphp/images/ajax-loader.gif' alt=''/>" );
				spGuiUpdateListBox( listboxinfo, db /*, dbtitle*/ );

				jQuery( infoselector ).html('');							// clear info for selected item
				container.find('.spEdit').attr( 'disabled', 'disabled' );	// disable edit button
				container.find('.spDelete').attr( 'disabled', 'disabled' );	// disable delete button
				container.find('.spTrash').attr( 'disabled', 'disabled' );	// disable delete button
			}
		});
	}
});	

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle delete
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery( 'body' ).on( 'click', '.spGuiListBoxNED .spDelete', function(e) {

	container = jQuery(this).closest('.spGuiListBoxNED');
	
	infoselector	= container.attr( 'data-infoselector' );
	db				= container.attr( 'data-database' );
//	dbtitle			= container.attr( 'data-database-title' );
	listboxinfo		= container.find( '.spListBoxInfo' );
	sel 			= container.find( '.spGuiListBoxItem[data-selected=true]' );
	name 			= sel.attr( 'data-name' );
	id 				= sel.attr( 'data-id' );
	
	if ( confirm( 'Are you sure you want to delete: '+name ) )
	{
		// delete
		spAjaxCallback({ 
			callback 	: "ajaxDelete", 
			param		: [
					{ name:'id',		value:id },
					{ name:'database', 	value:db }
				],
			title		: 'Deleting...',
			success		: function(){
				listboxinfo.html( "<img src='"+spHome+"/wp-content/plugins/sigmaphp/images/ajax-loader.gif' alt=''/>" );
				spGuiUpdateListBox( listboxinfo, db /*, dbtitle*/ );

				jQuery( infoselector ).html('');							// clear info for selected item
				container.find('.spEdit').attr( 'disabled', 'disabled' );	// disable edit button
				container.find('.spDelete').attr( 'disabled', 'disabled' );	// disable delete button
				container.find('.spTrash').attr( 'disabled', 'disabled' );	// disable delete button
			}
		});
	}
});	

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle new
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery( "body" ).on( "click", ".spGuiListBoxNED .spNew", function(e) {

	container = jQuery(this).closest('.spGuiListBoxNED');

	listboxid 		= container.attr( 'id' );
	infoselector	= container.attr( 'data-infoselector' );
	db				= container.attr( 'data-database' );
	fb				= container.attr( 'data-formbuilder' );
	sh				= container.attr( 'data-subheading' );
	
	spShowLoadingBar( infoselector );	
	
	jQuery(infoselector).load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: 'ajaxNewForm',
			database		: db,
			formbuilder		: fb,
			subheading		: sh,
			listboxid		: listboxid
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
			spInit();
		}
	);	
});	

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle save
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery( 'body' ).on( 'click', '.formBuilder .spSave', function(e) {

	spOpenDialog( "Saving..." );

	// from form container
	form = jQuery( this ).parents("form:first");
	listboxId 		= form.attr( "data-listboxid" );
	callbacksave 	= form.attr( "data-callbacksave" );
	ser 			= form.serialize();
	
	// from listbox container
	container = jQuery( "#"+listboxId );
	db 				= container.attr( 'data-database' );
//	dbtitle			= container.attr( 'data-database-title' );
	infoselector	= container.attr( 'data-infoselector' );
	listboxinfo		= container.find( '.spListBoxInfo' );
	sel 			= container.find( '.spGuiListBoxItem[data-selected=true]' );
	id 				= sel.attr( 'data-id' );

	jQuery.ajax({
		url 		: spHome+"/wp-admin/admin-ajax.php?action=sp_ajax_response",
		data 		: ser+"&formType="+jQuery( this ).parents("form:first").attr("name")+"&callback="+callbacksave+"&database="+db+"&id="+id,
		dataType 	: "json",
		type 		: "POST",
		success		: function(data){
			
			if ( data.formerrors!=undefined )
			{
				processFormErrors( data.formerrors );
				return;
			}
			
			// we need to update the listbox
			jQuery( infoselector ).html("");
			spShowLoadingBar( listboxinfo );
			spGuiUpdateListBox( listboxinfo, db /*, dbtitle*/ );
		},
		error 		: function(XMLHttpRequest, textStatus, errorThrown){
			jQuery( "#spErrorMsg" ).html( 
				"AJAX ERROR:<br/><textarea>"+XMLHttpRequest.status+"</textarea>" +
				"<textarea>"+textStatus+"</textarea>" +
				"<textarea>"+XMLHttpRequest.responseText+"</textarea>"+
				"<textarea>"+errorThrown+"</textarea>"
			);
			
			jQuery( "#spErrorMsg" ).html( XMLHttpRequest.responseText );
		},
		complete 	: function()
		{
			spCloseDialog();
		}
	});		

	return false;
});	

///////////////////////////////////////////////////////////////////////////////////////////////////
// handle edit
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery( 'body' ).on( 'click', '.spGuiListBoxNED .spEdit', function(e) {

	container = jQuery(this).closest('.spGuiListBoxNED');

	listboxid 		= container.attr( 'id' );
	infoselector	= container.attr( 'data-infoselector' );
	db				= container.attr( 'data-database' );
	fb				= container.attr( 'data-formbuilder' );
	sh				= container.attr( 'data-subheading' );
	
	sel 			= container.find( '.spGuiListBoxItem[data-selected=true]' );
	id 				= sel.attr( 'data-id' );
	
	spShowLoadingBar( infoselector );	
	
	jQuery(infoselector).load( 
		spHome+'/wp-admin/admin-ajax.php',
		{
			action 			: 'sp_ajax_response',
			formType		: 'loadonly',
			loadonly		: 'ajaxEditForm',
			database		: db,
			formbuilder		: fb,
			subheading		: sh,
			listboxid		: listboxid,
			id				: id
		},
		function complete(responseText, textStatus, XMLHttpRequest)
		{
			spInit();
		}
	);	

});

} // end init


///////////////////////////////////////////////////////////////////////////////////////////////////
// call init when ready
///////////////////////////////////////////////////////////////////////////////////////////////////

jQuery(document).ready(function(){
	spGuiListBoxNED.init();
});